<template>
    <div class="my-container">
        <!-- 已登录时的头部 -->
        <div class="header user-info" v-if="user">
            <div class="base-info">
                <div class="left">
                    <van-image :src="userInfo.photo" round fit="cover" class="tou-xiang" />
                    <span>{{userInfo.name}}</span>
                </div>
                <div class="right">
                    <van-button type="default" size="mini" round style="color: #666;">编辑资料</van-button>
                </div>
            </div>
            <div class="data-info">
                <div class="item">
                    <span class="count">{{userInfo.art_count}}</span>
                    <span class="text">头条</span>
                </div>
                <div class="item">
                    <span class="count">{{userInfo.follow_count}}</span>
                    <span class="text">关注</span>
                </div>
                <div class="item">
                    <span class="count">{{userInfo.fans_count}}</span>
                    <span class="text">粉丝</span>
                </div>
                <div class="item">
                    <span class="count">{{userInfo.like_count}}</span>
                    <span class="text">获赞</span>
                </div>
            </div>
        </div>

        <!-- 未登录时的头部 -->
        <div class="header not-login" v-else>
            <div class="logo-btn" @click="$router.push('/login')">
                <img src="~@/assets/mobile.png" alt="">
                <span>登录 / 注册</span>
            </div>
        </div>

        <!-- 收藏和历史 -->
        <van-grid :column-num="2" clickable class="grid-nav">
            <van-grid-item text="收藏" class="grid-item">
                <i slot="icon" class="iconfont icon-shoucang"></i>
            </van-grid-item>
            <van-grid-item text="历史" class="grid-item">
                <i slot="icon" class="iconfont icon-lishi"></i>
            </van-grid-item>
        </van-grid>

        <!-- 消息通知和小智同学 -->
        <van-cell-group class="other">
            <van-cell title="消息通知" is-link class="tit" />
            <van-cell title="小智同学" is-link class="tit" />
        </van-cell-group>

        <!-- 退出登录 -->
        <van-cell title="退出登录" clickable  class="logout-cell"  v-show="user" @click="onLogout" />

    </div>
</template>

<script>
import { mapState } from 'vuex'
import {getUserInfo} from '@/api/user.js'

export default {
    name: 'MyIndex',
    data() {
        return {
            userInfo: ''
        }
    },
    computed: {
        ...mapState(['user'])
    },
    methods: {
        // 退出登录
        onLogout() {
            this.$dialog.confirm({
                title: '确认退出吗？',
            }).then(() => {
                // on confirm
                // 退出后清除token，我们这里将token的值设为null就相当于清除
                this.$store.commit('setUser', null)
            }).catch(() => {
                // on cancel
            });
        },

        // 加载用户信息
        loadUserInfo() {
            getUserInfo().then(({data}) => {
                // console.log(data)
                this.userInfo = data.data
            }).catch((err) => {
                console.log(err)
            })
        }
    },
    created() {
        // 只有登录时才获取个人信息
        if(this.user) {
            this.loadUserInfo()
        }
    }

}
</script>

<style lang="less" scoped>
.my-container {
    .header {
        height: 360px;
        background-image: url("~@/assets/banner.png"); //css里使用@别名需要写成~@
        background-size: cover;
    }

    .not-login {
        display: flex;
        justify-content: center;
        align-items: center;

        .logo-btn {
            display: flex;
            flex-direction: column; //更改主轴方向
            justify-content: center;
            align-items: center;

            img {
                width: 132px;
                height: 132px;
                margin-bottom: 15px;
            }

            span {
                color: #fff;
                font-size: 28px;
            }
        }

    }

    .user-info {
        .base-info {
            height: 230px;
            padding: 75px 32px 23px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .left {
                display: flex;
                align-items: center;
                .tou-xiang {
                    width: 132px;
                    height: 132px;
                    border: 4px solid #fff;
                    margin-right: 22px;
                }

                span {
                    font-size: 30px;
                    color: #fff;
                }
            }
        }

        .data-info {
            height: 130px;
            display: flex;
            justify-content: space-around;

            .item {
                color: #fff;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;

                .count {
                    font-size: 36px;
                }

                .text {
                    font-size: 23px;
                }
            }
        }
    }

    .grid-nav {
        .grid-item {
            .iconfont {
                font-size: 45px;
            }

            .icon-shoucang {
                color: #eb5253;
            }

            .icon-lishi {
                color: #ff9d1d;
            }
        }
    }

    .other {
        .tit {
            font-size: 30px;
        }
    }

    .logout-cell {
        text-align: center;
        font-size: 30px;
        color: #d86262;
    }

}
</style>
